package com.e3.basdoc.employee.client;

import com.e3.basdoc.employee.model.EmployeeModel;
import com.e3.template.tgf.TGFEditorDialog;
import com.e3.template.tgf.TGFFormPanel;
import com.e3.web.core.client.GwtAsyncCallback;
import com.google.gwt.user.client.Element;

public class EmployeeEditorDialog extends TGFEditorDialog<EmployeeModel> {
	
	private EmployeeRPCServiceAsync service = EmployeeRPCService.Util.getInstance();

	@Override
	protected void onRender(Element parent, int pos) {
		super.onRender(parent, pos);
		this.setSize(600, 550);
	}
	
	@Override
	public String checkOnSave(EmployeeModel model) {
		String code = model.getCode();
		String name = model.getName();
		
		StringBuffer msgs = new StringBuffer();
		if (code == null || code.trim().length() == 0) {
			msgs.append(EmployeeFieldsConstant.INSTANCE.employee_code()+",");
		}
		
		if (name == null || name.trim().length() == 0) {
			msgs.append(EmployeeFieldsConstant.INSTANCE.employee_name()+",");
		}
		
		String s  = null;
		if(msgs.length()>0){
			s = msgs.substring(0,msgs.length()-1);
		}
		if(s != null){
			return EmployeeMessages.INSTANCE.msg_notnull(s);
		}
		return null;
	}

	@Override
	public TGFFormPanel<EmployeeModel> createFormPanel() {
		return new EmployeeFormPanel();
	}

	@Override
	public void executeNewService(final EmployeeModel model, GwtAsyncCallback<EmployeeModel> callback) {
		service.newEmployee(model, callback);
	}

	@Override
	public void executeModifyService(final EmployeeModel model, GwtAsyncCallback<EmployeeModel> callback) {
		service.modifyEmployee(model, callback);
	}

	@Override
	public String[] getNotNullFieldNames() {
		return EmployeeHelper.INSTANCE.getNotNullFieldNames();
	}

}
